$(function () {
    $("#jqGrid").jqGrid({
        url:  baseURL + 'sys/noticeSetting/findAll',
        datatype: "json",
        colModel: [			
			{ label: '编号', name: 'id', width: 20, key: true,hidden:true },
			{ label: '标题', name: 'content', width: 200 },
			//{ label: '公告详情', name: 'title', width: 200 },
			{ label: '创建时间', name: 'creTime', width: 200 },
			{  label: '操作', name: 'id', index: 'id', width: 200, 
			     formatter: function (cellvalue, options, rowObject) { 
			     	return "<a  class='fa fa-pencil-square-o btn btn-primary' href=\"#\" onclick=\"Modify(" + options.rowId + ")\">查看详情</a> <a  class='fa fa-pencil-square-o btn btn-primary' href=\"#\" onclick=\"up(" + options.rowId + ")\">修改</a>" 
			     }  
			  },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
});
function Modify(id) {  
	//alert(id);  
	if(id == null){
		return ;
	}	
	$("#newt").modal("show");
	vm.title = "公告详情";
	vm.getUser(id);
}  
function up(id) {  
	//alert(id);
	setContent("");
	vm.update(id);
	
}  
function upload() {  
    new AjaxUpload('#upload', {
        action: baseURL + 'sys/oss/upload?token=' + token,
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
//            if(vm.config.type == null){
//                alert("云存储配置未配置");
//                return false;
//            }
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
//                alert(r.url);
            	var html = "<img src="+r.url+" _src="+r.url+">"
            		
            	um.execCommand('insertHtml', html)
	            $(".edui-close").click();	
            }else{
                alert(r.msg);
            }
        }
    });
}  
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			content: null
		},
		showList: true,
		title: null,
		noticeSetting: {},
		config: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			$("#id").val("");
			$("#content").val("");
			setContent("");
			vm.noticeSetting = {};
		},
		update: function (id) {
			//var id = getSelectedRow();
			if(id == null){
				return ;
			}
			$.get("/sys/noticeSetting/info/"+id, function(r){
				vm.showList = false;
                vm.title = "修改";
                $("#id").val(r.noticeSetting.id);
                $("#content").val(r.noticeSetting.content);
                um.execCommand('insertHtml', decodeURIComponent(r.noticeSetting.title))
                r.noticeSetting.title
				vm.noticeSetting = r.noticeSetting;
			});
			/*$.ajax({
		        type: "POST",
		        url: "/sys/noticeSetting/info/"+id,
		        contentType: "application/json;charset=utf-8",
		        dataType: "json",
		        success: function (r) {
		        	vm.showList = false;
	                vm.title = "修改";
					vm.noticeSetting = r.noticeSetting;
		        },
		    });*/
		},
		saveOrUpdate: function () {
//			var title = UM.getEditor('myEditor').getHtml();
			var title = $("#myEditor").html()+"";
			var id = $("#id").val();
			var content = $("#content").val();
		
			vm.noticeSetting.title = encodeURIComponent(title);
			vm.noticeSetting.content = content;
            if(vm.validator()){
                return ;
            }
			var url = vm.noticeSetting.id == null ? "sys/noticeSetting/save" : "sys/noticeSetting/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType:"application/json",
			    data: JSON.stringify(vm.noticeSetting),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getUser: function(id){
			$.get(baseURL + "sys/noticeSetting/info/"+id, function(r){
				
				$("#xiangq").html(decodeURIComponent(r.noticeSetting.title));
				//uParse("#xiangq",r.noticeSetting.title);
				/*uParse('#xiangq', {
				    rootPath: '../'
				})*/
			});
		},	
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/noticeSetting/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'content': vm.q.content},
                page:page 
            }).trigger("reloadGrid");
		},
        validator: function () {
           if(isBlank(vm.noticeSetting.content)){
                alert("标题不能为空");
                return true;
            }

            if(isBlank(vm.noticeSetting.title)){
                alert("公告详情不能为空");
                return true;
            }

        }
	}
});
function HTMLEncode(html) 
{ 
var temp = document.createElement ("div"); 
(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html); 
var output = temp.innerHTML; 
temp = null; 
return output; 
} 

function HTMLDecode(text) 
{ 
var temp = document.createElement("div"); 
temp.innerHTML = text; 
var output = temp.innerText || temp.textContent; 
temp = null; 
return output; 
} 